package com.lty.springmvc.converter;

import java.util.Objects;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.lty.springmvc.crud.entity.Department;
import com.lty.springmvc.crud.entity.Employee;

@Component
public class EmployeeConverter implements Converter<String, Employee> {

	@Override
	public Employee convert(String source) {
		if(!StringUtils.isEmpty(source)){
			//xiaofang-xiaofang@163.com-0-105
			String[] employeeArr = source.split("-");
			if(Objects.nonNull(employeeArr) && employeeArr.length == 4){
				String lastName = employeeArr[0];
				String email = employeeArr[1];
				Integer gender = Integer.valueOf(employeeArr[2]);
				Department department = new Department();
				department.setId(Integer.valueOf(employeeArr[3]));
				
				
				Employee employee = new Employee(null, lastName, email, gender, department);
				System.out.println(source + "-----converter-----" + employee);
				return employee;
			}
		}
		
		return null;
	}

}
